﻿using UnityEngine;
using UnityEngine.UI;

public class TimeSlider : MonoBehaviour {
	private static TimeSlider instance;
	public float minusSpeed = 0.0005f;
	public Image slider;
	public Color startColor;
	public Color endColor;

	private void Awake()
	{
		instance = this;
	}

	void Update()
	{
		if (!GameManager.IsPlaying||GameStatistic.GameScore<1)
			return;
		slider.fillAmount -= minusSpeed;
		if (slider.fillAmount <= 0) {
			if (GameManager.gameStage != GameStage.GAMEOVER) {
				timer += Time.deltaTime;
				if (timer >= 2) {
					GameManager.gameStage = GameStage.GAMEOVER;
					AudioManager.PlayOneShoot (4);
				}
			}
		}
//		slider.color = Color32.Lerp (startColor, endColor, minusSpeed*2);
//		print (Color.Lerp (startColor, endColor, minusSpeed*2));
		colorTimer+=0.0025f;
		color = Color32.Lerp(startColor,endColor,colorTimer);
		slider.color = color;
	}
	private float colorTimer;
	private Color32 color;
//	private Color32 green = Color.green;
//	private Color32 red = Color.red;
	private float timer;

	public static bool IsCanPlay
	{
		get { return instance.slider.fillAmount > 0;}
	}
	public static void Reset()
	{
		instance.timer = 0;
		instance.slider.fillAmount = 1;
		instance.slider.color = instance.startColor;
		instance.colorTimer = 0;
	}
}
